package svc

import (
	"github.com/zeromicro/go-zero/core/stores/redis"
	"github.com/zeromicro/go-zero/zrpc"
	"mall-backend/api/internal/config"
	client "mall-backend/rpc/goods/client/goods"
	"strings"
	"time"
)

type ServiceContext struct {
	Config     config.Config
	StoreRedis *redis.Redis
	GoodsRpc   client.Goods
}

func NewServiceContext(c config.Config) *ServiceContext {
	conf := redis.RedisConf{
		Host:        strings.Join(c.RedisCluster.Cluster.Addrs, ","),
		Type:        redis.NodeType,
		Pass:        c.RedisCluster.Cluster.Password,
		Tls:         false,
		NonBlock:    false,
		PingTimeout: time.Second,
	}
	storeRedis := redis.MustNewRedis(conf)
	return &ServiceContext{
		Config:     c,
		StoreRedis: storeRedis,
		GoodsRpc:   client.NewGoods(zrpc.MustNewClient(c.GoodsRpc)),
	}
}
